package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.config.annotation.DataScope;
import com.dragon.repository.entity.Deliver;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-26
 */
@Mapper
public interface DeliverMapper extends BaseMapper<Deliver> {

    @Select("<script>"+
            "SELECT M.* FROM (SELECT a.*, d.username AS deliveryName , c.customer_name AS customerName, " +
            " e.name as contractName " +
            "FROM dragon_deliver a " +
            "LEFT JOIN dragon_customer c on a.customer_id = c.id " +
            "LEFT JOIN dragon_admin d on d.id = a.delivery_id " +
            "LEFT JOIN dragon_contract e on e.id = a.contract_id " +
            "ORDER BY a.create_time DESC ) M WHERE 1=1  " +
            "<if test='search!=null and search != \"\"'> " +
            "AND (M.customerName like CONCAT('%',#{search},'%')) or (M.address like CONCAT('%',#{search},'%')) "+
            "</if> "+
            "<if test='status!=null '> " +
            "AND M.status = #{status} "+
            "</if> "+
            "<if test='type!=null '> " +
            "AND M.type = #{type} "+
            "</if> "+
            "</script> ")
    @DataScope(adminAlias = "M", adminField = "delivery_id", deptAlias = "M", deptField = "department_id",authCodeString = "deliverManagement")
    Page<Deliver> selectDelivery(Page<Deliver> deliverPage, String search, Integer status, Integer type);

    @Select("SELECT M.* FROM " +
            "(SELECT a.*, d.username AS deliveryName , b.name AS produceName,c.customer_name AS customerName, e.name as contractName " +
            "FROM dragon_deliver a " +
            "LEFT JOIN dragon_produce b on a.produce_id = b.id " +
            "LEFT JOIN dragon_customer c on a.customer_id = c.id " +
            "LEFT JOIN dragon_admin d on d.id = a.delivery_id " +
            "LEFT JOIN dragon_contract e on e.id = a.contract_id ) M  " +
            "${ew.customSqlSegment}")
    List<Deliver> selectDeliveryList(@Param(Constants.WRAPPER) QueryWrapper<Deliver> wrapper);

    @Select("SELECT M.* FROM " +
            " (SELECT a.*, d.username AS deliveryName , b.name AS produceName,c.customer_name AS customerName, e.name as contractName " +
            " FROM dragon_deliver a " +
            " LEFT JOIN dragon_produce b on a.produce_id = b.id" +
            " LEFT JOIN dragon_customer c on a.customer_id = c.id " +
            " LEFT JOIN dragon_admin d on d.id = a.delivery_id " +
            " LEFT JOIN dragon_contract e on e.id = a.contract_id ) M where 1=1 and M.id = #{id} ")
    Deliver selectDeliveryById(Integer id);

    /**
     * 送货 年数量
     * @return
     */
    @Select("SELECT count(*)  FROM dragon_deliver a " +
            "left join dragon_contract dc on a.contract_id = dc.id " +
            "where a.status = 1 and dc.status = 1 and dc.deleted = 0  ")
    @DataScope( adminAlias = "a",adminField = "admin_id",deptAlias = "a",deptField = "department_id",  authCodeString = "deliverManagement")
    Long selectYearDeliverCount();

    /**
     * 送货 月数量
     * @return
     */
    @Select("SELECT count(*)  FROM dragon_deliver a " +
            "left join dragon_contract dc on a.contract_id = dc.id " +
            "where a.status = 1 and dc.status = 1 and dc.deleted = 0 and left(dc.order_time, 7) = left(Now() ,7) ")
    @DataScope( adminAlias = "a",adminField = "admin_id",deptAlias = "a",deptField = "department_id",  authCodeString = "deliverManagement")
    Long selectMonthDeliverCount();

    @Select("SELECT M.* FROM " +
            " (SELECT a.*, d.username AS deliveryName , da.username as applicantName, b.name AS produceName,c.customer_name AS customerName, e.name as contractName " +
            " FROM dragon_deliver a " +
            " LEFT JOIN dragon_produce b on a.produce_id = b.id" +
            " LEFT JOIN dragon_customer c on a.customer_id = c.id " +
            " left join dragon_admin da on da.id = a.admin_id "+
            " LEFT JOIN dragon_admin d on d.id = a.delivery_id " +
            " LEFT JOIN dragon_contract e on e.id = a.contract_id ORDER BY a.create_time DESC ) M where 1=1 and M.contract_id = #{contractId} ")
    Page<Deliver> selectPageAndContract(Page<Deliver> deliverPage, Integer contractId);
}
